<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:general="http://java.sun.com/jsf/composite/components/general">

    <ui:composition template="template.xhtml">
        <ui:define name="dialogs">

             <general:confirmDialog
                widgetDialog="confirmRemoveDialog"
                dialogTitle="Remoção de obra"
                message="Deseja realmente remover a obra selecionada?"
                buttonYesAction="#{obraBean.delete()}"
                onclick_no_javascript="confirmRemoveDialog.hide()"
                onclick_yes_javascript="confirmRemoveDialog.hide()"
                onclick_yes_update=":growl :obraForm"/>

            <p:dialog header="Cadastro de obra" widgetVar="newObra" modal="true" resizable="false">
                <p:panel id="panelObra" header="Obra" style="width: 560px">

                    <h:form id="newObraform">
                          <h:panelGrid columns="2">
                            <h:outputText value="Nome obra: " styleClass="formShortLabel"  />
                            <p:inputText value="#{obraBean.obra.name}"  style="width: 382px"/>
                            
                             <h:outputText value="Engenheiro:" styleClass="formShortLabel"/>
                             <p:autoComplete value="#{obraBean.obra.engenheiro}" 
                                completeMethod="#{autoCompleteBean.completeEngenheiro}"
                                        var="c" itemLabel="#{c.name}" itemValue="#{c}"
                                        converter="engenheiroConverter" size="65" />
                             
                            <h:outputText value="Endereço:" styleClass="formShortLabel" />
                            <p:inputText value="#{obraBean.obra.endereco}" style="width: 382px"/>
                          </h:panelGrid>
                          
                          <h:panelGrid columns="4">
                            <h:outputText value="Bairro:" styleClass="formShortLabel"  />
                            <p:inputText value="#{obraBean.obra.bairro}" />
                            
                             <h:outputText value="Cidade:" styleClass="formShortLabel"/>
                             <p:autoComplete value="#{obraBean.obra.cidade}"
                                             completeMethod="#{autoCompleteBean.completeCidade}"
                                        var="c" itemLabel="#{c.name}" itemValue="#{c}"
                                        converter="cidadeConverter"  />
                             
                             <h:outputText value="CEP:" styleClass="formShortLabel" />
                             <p:inputText value="#{obraBean.obra.cep}"  />
                             <h:outputText value="Telefone:"  styleClass="formShortLabel"/>
                             <p:inputText value="#{obraBean.obra.telefone}" />
                             <h:outputText value="Data Cadastro:"  styleClass="formShortLabel"/>
                             <p:calendar value="#{obraBean.obra.dataCadastro}"/>
                             
                             <h:outputText value="Situação:" styleClass="formShortLabel"/>
                             <p:autoComplete value="#{obraBean.obra.situacao}"
                                             completeMethod="#{autoCompleteBean.completeSituacaoObra}"
                                        var="c" itemLabel="#{c.name}" itemValue="#{c}"
                                        converter="situacaoObraConverter"  />
                             <h:outputText value="Início obra:"  styleClass="formShortLabel"/>
                             <p:calendar value="#{obraBean.obra.inicioObra}"/>
                             <h:outputText value="Fim obra:"  styleClass="formShortLabel"/>
                             <p:calendar value="#{obraBean.obra.fimObra}"/>
                          </h:panelGrid>
                        
                        <p:separator />
                        <p:commandButton value="Salvar" action="#{obraBean.save()}" update=":obraForm :growl" />
                    </h:form>

                </p:panel>
            </p:dialog>
        </ui:define>



        <ui:define name="content">
            <h:form id="obraForm">
              <h:panelGrid columns="2">
                        <h:outputText value="Nome:" styleClass="formLabel" style="width: 120px"/>
                        <p:inputText id="obraName" style="width: 300px" value="#{obraBean.obraFilterName}"
                                     onclick="jQuery(this).select()">
                            <p:ajax event="keyup" update="obraList" />
                        </p:inputText>

                        <p:watermark for="obraName" value="Digite o nome da obra" />
                    </h:panelGrid>
              <h:panelGroup id="obraList">
                  <p:dataTable var="d" value="#{obraBean.listObras()}"
                               paginator="true" rows="10" rowKey="#{d.id}"
                                     emptyMessage="Nenhuma obra foi encontrada"
                                     selectionMode="single" selection="#{obraBean.obraSelected}">

                            <p:ajax event="rowSelect" update="@this"/>

                            <f:facet name="header">
                                Lista de obras
                            </f:facet>

                            <p:column style="width: 70%">
                                <f:facet name="header">
                                    <h:outputText value="Nome" />
                                </f:facet>
                                <h:outputText value="#{d.name}" />
                            </p:column>


                            <f:facet name="footer">
                                <p:commandButton icon="ui-icon ui-icon-plus" title="Adicionar obra" update=":newObraform" oncomplete="newObra.show()"
                                                 disabled="#{not sharedPermissionManagedBean.hasPermission('CRIAR_OBRA')}"/>
                                <p:commandButton icon="ui-icon ui-icon-pencil" title="Editar obra" 
                                                 disabled="#{(not sharedPermissionManagedBean.hasPermission('EDITAR_OBRA')) or (obraBean.obraSelected.id == null)}"/>

                                <p:commandButton icon="ui-icon ui-icon-minus" title="Remover obra" onclick="confirmRemoveDialog.show()"
                                                 disabled="#{(not sharedPermissionManagedBean.hasPermission('REMOVER_OBRA')) or (obraBean.obraSelected.id == null)}"/>

                            </f:facet>
                        </p:dataTable>
                    </h:panelGroup>
            </h:form>
        </ui:define>
    </ui:composition>
</html>

